package com.smartEdu.service.impl.app;

import java.util.List;
import java.util.Map;

import org.osgi.framework.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.smartEdu.commons.CMap;
import com.smartEdu.commons.LogCvt;
import com.smartEdu.commons.Session;
import com.smartEdu.commons.Tag_Tips;
import com.smartEdu.dao.ArticleDao;
import com.smartEdu.exception.JsonException;
import com.smartEdu.model.ArticleModel;
import com.smartEdu.service.ArticleService;

@Service(value="articleServiceImpl")
public class ArticleServiceImpl extends BaseServiceImpl implements ArticleService{
	@Autowired
	@Qualifier(value="articleDaoImpl")
	private ArticleDao artilceDao;
	
	@Override
	public void recommend(Session sess) {
		List<ArticleModel> articlelist=null;
		try {
			articlelist=artilceDao.getArticle();
			sess.put(Tag_Tips.ARTICLE, articlelist);
			sess.put(Tag_Tips.RESPCODE, Tag_Tips.OK);
		}
		catch (JsonException e){
			LogCvt.error(Tag_Tips.JSONERROR,e);
			throw new ServiceException("News业务流程出错");
		}
		catch (Exception e) {
			LogCvt.error(e.getMessage(),e);
			throw new ServiceException("News业务流程出错");
		}
		finally {
		}
	}
	
	@Override
	public void showdetail(Session sess) {
		Map<String, Object> content=null;
		CMap map=null;
		try {
			map=SetToMap(sess);
			content=artilceDao.getArticleDetail(map.gets("pk"));
			sess.put("content", content.get("CONTENT").toString());
			sess.put(Tag_Tips.RESPCODE, Tag_Tips.OK);
		}
		catch (JsonException e){
			LogCvt.error(Tag_Tips.JSONERROR,e);
			throw new ServiceException("News业务流程出错");
		}
		catch (Exception e) {
			LogCvt.error(e.getMessage(),e);
			throw new ServiceException("News业务流程出错");
		}
		finally {
		}
	}

	@Override
	public void getArticleByContent(Session sess) {
		// TODO Auto-generated method stub
		List<ArticleModel> articlelist=null;
		CMap map=null;
		try {
			map=SetToMap(sess);
			articlelist=artilceDao.getArticleByContent(map.gets("title"));
			System.out.println("title:"+map.gets("title"));
			if(articlelist.size()==0){
				sess.put(Tag_Tips.RESPCODE,2);
				sess.put(Tag_Tips.ARTICLE, articlelist);
			}else{
			   sess.put(Tag_Tips.ARTICLE, articlelist);
			   sess.put(Tag_Tips.RESPCODE, Tag_Tips.OK);
			}
		}
		catch (JsonException e){
			LogCvt.error(Tag_Tips.JSONERROR,e);
			throw new ServiceException("News业务流程出错");
		}
		catch (Exception e) {
			LogCvt.error(e.getMessage(),e);
			throw new ServiceException("News业务流程出错");
		}
		finally {
		}
		
	}
	
}
